from ttzScrapyProject.constants.UrlConstant import CHARGE_CARD_URL
from ttzScrapyProject.dao.BaseDao import MysqlDao
from ttzScrapyProject.utils import HttpUtils, DateUtils
from ttzScrapyProject.vo.LoginUserVo import UserVo


class ChargeDao(MysqlDao):
    def __init__(self):
        super().__init__()
        self.cursor = self.getCursor()
        self.db = self.getDB()
        self.tableName = 'charge'

    def chargeUser(self, loginUserVo,userToken):
        try:
            # url = f"http://webapicname.coo456.com/apiNew/batchUseFuLiCard?token=e7c69a02e8937177324ed46188893bfe&client=app&Content=1184391273316d*omsf91439127331866rkuy"
            content = ""
            # 1. 查询用户当前未充值使用的卡密
            notUseSql = f"select * from user_card_history where  USER_PHONE ={loginUserVo.phone}	 and STATUS = 0  and CARD_NUM_AND_PASSWORD is not null"
            self.cursor.execute(notUseSql)
            content = self.cursor.fetchall()
            if not content:
                print(f"f{loginUserVo.phone} 没有待充值卡密,跳过充值流程")
                return
            for eachContent in content:
                tempContent = eachContent[3]
                chargeUseurl = f"{CHARGE_CARD_URL}{userToken}&client=app&Content={tempContent}"
                chargeInfoResp = HttpUtils.get("账号充值卡密", chargeUseurl)
                if chargeInfoResp["Data"] is not None:
                    updateSql = "UPDATE scrapy_user_game.user_card_history SET   STATUS=1,   UPDATED_TIME=%s WHERE USER_PHONE=%s and CARD_NUM_AND_PASSWORD=%s"
                    values = (DateUtils.getDate(),loginUserVo.phone,tempContent)
                    self.cursor.execute(updateSql,values)
                self.db.commit()
        except:
            print(f"{loginUserVo.phone} 充值卡密失败")

if __name__ == '__main__':
    vo = UserVo()
    vo.phone="17120572204"
    ChargeDao().chargeUser(loginUserVo=vo,userToken="f667e96528012cf7987a1eae0a075e57")
